import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store'
const routes = [
  {
    path: '/auth',
    name: 'Auth',
    component: () => import(/* webpackChunkName: "Auth" */ '../views/Auth.vue')
  },
  {
    path: '/',
    name: 'BasicLayout',
    component: () =>
      import(/* webpackChunkName: "BasicLayout" */ '../layout/BasicLayout.vue'),
    children: [
      {
        path: 'goods',
        name: 'Goods',
        component: () =>
          import(/* webpackChunkName: "Goods" */ '../views/Goods/index.vue')
      },
      {
        path: 'goods/:id',
        name: 'GoodDetail',
        component: () =>
          import(/* webpackChunkName: "GoodDetail" */ '../views/GoodDetail.vue')
      },
      {
        path: '/users',
        name: 'Users',
        component: () =>
          import(/* webpackChunkName: "Users" */ '../views/Users/Users.vue')
      },
      {
        path: '/log',
        name: 'Log',
        component: () =>
          import(/* webpackChunkName: "Log" */ '../views/Log.vue')
      },
      {
        path: '/reset/password',
        name: 'ResetPassword',
        component: () =>
          import(
            /* webpackChunkName: "ResetPassword" */ '../views/ResetPassword.vue'
          )
      },
      {
        path: '/inviteCode',
        name: 'InviteCode',
        component: () =>
          import(/* webpackChunkName: "InviteCode" */ '../views/InviteCode.vue')
      },
      {
        path: '/classify',
        name: 'GoodClassify',
        component: () =>
          import(
            /* webpackChunkName: "GoodClassify" */ '../views/GoodClassify.vue'
          )
      },
      {
        path: '/profile',
        name: 'Profile',
        component: () =>
          import(/* webpackChunkName: "Profile" */ '../views/Profile.vue')
      },
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () =>
          import(/* webpackChunkName: "Profile" */ '../views/Dashboard.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach(async (to, from, next) => {
  // const reqArr = []
  // if (store.state.characterInfo.length === 0) {
  //   reqArr.push(store.dispatch('getCharacterInfo'))
  // }
  // if (!store.state.userInfo.username) {
  //   reqArr.push(store.dispatch('getUserInfo'))
  // }
  // await Promise.all(reqArr)

  if (store.state.characterInfo.length === 0) {
    await store.dispatch('getCharacterInfo')
  }
  if (!store.state.userInfo.username) {
    await store.dispatch('getUserInfo')
  }
  next()
})

export default router
